package com.sc.nft.controller;


import com.sc.nft.annotation.UserLogin;
import com.sc.nft.controller.request.IdRequest;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.dto.ActivityShareTeamPopUpDetailDTO;
import com.sc.nft.entity.dto.MallActivityShareTeamDetailDTO;
import com.sc.nft.entity.dto.MallActivityShareTeamPageDTO;
import com.sc.nft.entity.dto.MallShareTeamOrderPageDTO;
import com.sc.nft.entity.vo.MallActivityShareTeamPageVO;
import com.sc.nft.entity.vo.MallShareTeamOrderPageVO;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.MallActivityShareTeamService;
import com.sc.nft.service.MallShareTeamOrderService;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 分享购团队信息 前端控制器
 * </p>
 *
 * @author ligen
 * @since 2023-12-13
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/ipMall/activity/share/team")
@Api(tags = "分享购团队模块")
public class MallActivityShareTeamController {

    private final MallActivityShareTeamService mallActivityShareTeamService;
    private final MallShareTeamOrderService mallShareTeamOrderService;
    private final Redisson redisson;

    @ApiOperation(value = "用户开团 ID传分享购活动ID", notes = "用户开团 ID传分享购活动ID", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/start", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<MallActivityShareTeamDetailDTO> userStartShareTeam(@Valid @RequestBody IdRequest idRequest) {
        UserInfo userInfo = UserPool.getUser();
        // 用户开团
        RLock lock = redisson.getLock(RedisKeyEnum.USER_GROUP_LOCK.getKey() + userInfo.getId());
        MallActivityShareTeamDetailDTO mallActivityShareTeamDetailDTO = null;
        try {
            boolean res = lock.tryLock(3, 10, TimeUnit.SECONDS);
            if (res) {
                mallActivityShareTeamDetailDTO = mallActivityShareTeamService.userStartShareTeam(UserPool.getUser(), idRequest.getId());
            }else {
                return Result.fail(ErrorCode.LATER_TRY);
            }
        } catch (InterruptedException e) {
            return Result.fail("已开团，请稍后再试");
        } finally {
            lock.unlock();
        }
        return Result.success(mallActivityShareTeamDetailDTO);
    }

    @ApiOperation(value = "用户分享成团列表", notes = "用户分享成团列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/page", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<MallActivityShareTeamPageDTO> getMallActivityShareTeamPageDTO(@RequestBody MallActivityShareTeamPageVO mallActivityShareTeamPageVO) {
        return PageResult.success(mallActivityShareTeamService.getMallActivityShareTeamPageDTO(UserPool.getUser(), mallActivityShareTeamPageVO));
    }

    @ApiOperation(value = "用户成团详情 传成团主键ID", notes = "用户成团详情 传成团主键ID", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/detail", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<MallActivityShareTeamDetailDTO> getMallActivityShareTeamDetailDTO(@RequestBody IdRequest idRequest) {
        return Result.success(mallActivityShareTeamService.getMallActivityShareTeamDetailDTO(UserPool.getUser(), idRequest.getId()));
    }

    @ApiOperation(value = "获取分享团队弹窗信息 传成团主键ID", notes = "获取分享团队弹窗信息 传成团主键ID", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/popUpDetail", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<ActivityShareTeamPopUpDetailDTO> getActivityShareTeamPopUpDetailDTO(@RequestBody IdRequest idRequest) {
        return Result.success(mallActivityShareTeamService.getActivityShareTeamPopUpDetailDTO(idRequest.getId()));
    }

    @ApiOperation(value = "用户分享成团订单列表 传成团主键ID", notes = "用户分享成团列表 传成团主键ID", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/order/page", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<MallShareTeamOrderPageDTO> getMallShareTeamOrderPageDTO(@Valid @RequestBody MallShareTeamOrderPageVO mallShareTeamOrderPageVO) {
        return PageResult.success(mallShareTeamOrderService.getMallShareTeamOrderPageDTO(mallShareTeamOrderPageVO));
    }
}
